﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace JQueryEasyUI
{
    public class Tools
    {
       /// <summary>
        /// 重造一个DataTable,让其只有"id","title"两列，行数据分别来自idColumnName、titleColumnName指定的列名。
       /// </summary>
       /// <param name="dt"></param>
       /// <param name="idColumnName"></param>
       /// <param name="titleColumnName"></param>
       /// <returns></returns>
        public static DataTable Rebuild(DataTable dt,string idColumnName,string titleColumnName)
        {
            DataTable newdt = new DataTable();
            newdt.Columns.Add(new DataColumn("id", typeof(string)));
            newdt.Columns.Add(new DataColumn("title", typeof(string)));
            foreach (DataRow item in dt.Rows)
            {
                DataRow newrow = newdt.NewRow();
                newrow["id"] = item[idColumnName];
                newrow["title"] = item[titleColumnName];
                newdt.Rows.Add(newrow);
            }
            return newdt;
        }

        /// <summary>
        /// 调用方式如：ToJson(dt,"colname1","colname2"...)，GetJson(dt,null)则生成全部列。返回 [{'id':12,'column1':'12'},{'id':13,'column1':'12'}]格式的json
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="columnNames"></param>
        /// <returns></returns>
        public static string ToJson(DataTable tableSource, params string[] columnNames)
        {


            if (tableSource == null )
				{
					return "";
				}
            if (tableSource.Rows.Count== 0)
            {
                return "";
            }
				int columnCount = tableSource.Columns.Count;
				int rowCount = tableSource.Rows.Count;
                if (columnNames == null || columnNames.Length==0)
				{
					
					columnNames = new string[columnCount - 1+ 1];
					System.Text.StringBuilder aa = new System.Text.StringBuilder();
					for (int i = 0; i <= columnCount - 1; i++)
					{
						
						// aa.AppendLine("<th field=\"" + tableSource.Columns[i].ColumnName + "\" width=\"100\">" + tableSource.Columns[i].ColumnName + "</th>");
						//aa.AppendLine("{field:\"" + tableSource.Columns[i].ColumnName + "\",title:\"" + tableSource.Columns[i].ColumnName + "\",width:80},");
						columnNames[i] = tableSource.Columns[i].ColumnName;
						// string cc = aa.ToString();
					}
				}
				if (columnNames.Length < 1)
				{
					return "";
				}
				string jsonData = "[";
				
				if (rowCount > 0)
				{
					foreach (DataRow row in tableSource.Rows)
					{
						jsonData += "{";
						for (int i = 0; i <= columnNames.Length - 1; i++)
						{
							string filedData = row[columnNames[i]].ToString();
							filedData = filedData.Replace("\\", "/");
							jsonData += "\"" + columnNames[i] + "\":\"" + filedData + "\",";
						}
						jsonData = jsonData.Substring(0, jsonData.Length - 1);
						jsonData += "},";
					}
					jsonData = jsonData.Substring(0, jsonData.Length - 1);
					jsonData += "]";
				}
				else
				{
					jsonData += "]";
				}
				return jsonData;
			
        
        }

    }
}
